package com.polaris.live.ui.recharge

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.polaris.live.adapter.recharge.RechargeViewPageAdapter
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.databinding.ActivityRechargeBinding
import com.polaris.live.resp.event.RechargeTodayEvent
import com.polaris.live.translation.R.string
import com.polaris.live.utils.getIntOrString
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.RechargeViewModel
import org.greenrobot.eventbus.EventBus

/**
 * RechargeActivity
 *
 * @author Created by 半仙 on 2023/9/4/004 14:58
 */
class RechargeActivity : BaseVbActivity<RechargeViewModel, ActivityRechargeBinding>() {

    private val mInitPage by lazy { intent.getIntOrString("page", 0) }

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.acLlTitle)
        setStatusBarDarkFont(false)
        setNavigationBarDarkFont(false)
        attachImmersionBar { transparentNavigationBar() }
    }

    override fun initView(savedInstanceState: Bundle?) {
        val tabs = mutableListOf<String>()
        tabs.add(getString(string.recharge_title))
        tabs.add(getString(string.recharge_game_beans))
        val list = mutableListOf<Fragment>()
        list.add(RechargeFragment())
        list.add(GameBeansFragment())
        mBinding.viewpager2.adapter = RechargeViewPageAdapter(list, this)
        TabLayoutMediator(
            mBinding.tab, mBinding.viewpager2
        ) { tab: TabLayout.Tab, p: Int ->
            tab.text = tabs[p]
        }.attach()

        mBinding.viewpager2.setCurrentItem(mInitPage, false)
        initListener()
    }

    private fun initListener() {
        mBinding.apply {
            acIvBack.setOnSingleClickListener {
                finish()
            }
            acIvDiamond.setOnSingleClickListener {
                val position = mBinding.viewpager2.currentItem
                if (position == 0) {
                    Router.builder(RoutePath.DIAMOND_ACTIVITY)
                        .navigation(this@RechargeActivity)
                } else {
                    Router.builder(RoutePath.DIAMOND_ACTIVITY)
                        .withBoolean("isGameDetail", true)
                        .navigation(this@RechargeActivity)
                }
            }
        }
    }

    override fun onDestroy() {
        if (mBinding.viewpager2.currentItem == 0) {
            EventBus.getDefault().post(RechargeTodayEvent())
        }
        super.onDestroy()
    }
}